package {
import kgame5.kutil.KEvent;

import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.TextField;

/**
 * @author kk
 * 整个的一个面板
 * 
 * --用法
 * 
 * //1
 * var loadingBar:LoadingBar2;
 * 
 * //2,
 * loadingBar=new LoadingBar2();
		loadingBar.addEventListener(LoadingBar2.EVENTTYPE_LOADOK, onLoadLodingBarOK);
		addChild(loadingBar);
		
		loadingBar.start(userName);//启动
	
	
	//3,加载进度条
	private function onLoadLodingBarOK(e:Event){
		//-加载图形
		this.b6();
	}
	
	
 * 
 * 
 */
public class LoadingBar extends Sprite{
	//public static const EVENTTYPE_LOADOK:String="EVENTTYPE_LOADOK";
	private var 
		mc:MovieClip,
			mc_loading:MovieClip,
				mc_bar:MovieClip,
				tf_p:TextField,
			tf_tip:TextField;
				
	private var loader:Loader;
	
	public function LoadingBar(loadingSkinUrl:String){
		//T.p("LoadingBar2");
		loader=new Loader();
		
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadOK);
		
		start(loadingSkinUrl);
	}
	public function start(url:String):void{
		loader.load(new URLRequest(url));
	}
	private function onLoadOK(e:Event):void{
		//注意e.target是ContentLoaderInfo,可不是loader啊
		//T.p(this,"onLoadOK");
		
		mc=loader.content as MovieClip;

		mc_loading=mc.mc_loading;
		
		//mc_loading.x=this.stage.stageWidth/2-mc_loading.width/2;
		//mc_loading.y=this.stage.stageHeight/2-mc_loading.height/2;
		
		mc_bar=mc_loading.mc_bar;
		tf_p=mc_loading.tf_p;
		
		tf_tip=mc.tf_tip;
		
		update(0, 0);
		
		addChild(mc);//添加
		
		//this.dispatchEvent(new KEvent(EVENTTYPE_LOADOK));
		
	}
	
	public function setTip(s:String):void{
		if(tf_tip!=null){
			tf_tip.text=s;
		}
	}
	public function update(bytesLoaded:int,bytesTotal:int):void{
		if(mc_loading==null){
			return;
		}
		var p:Number=0;
		if(bytesTotal==0){
			p=0;
		}else{
			p=bytesLoaded/bytesTotal;
		}
		
		mc_bar.scaleX=p;
		tf_p.text=int(p*100)+"";
		
	}
	
}
}
